package com.bbcare.followup.plat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.SeqUtil;
import com.bbcare.comm.ToolUtils;
import com.bbcare.followup.plat.service.ISchemeService;

/*
 * @name:         方案管理控制类
 * @author:       DemonLee
 * @createTime:   2017.4.26
 * @description:  方案新增，查询及配置等相关操作
 * @modify:
 *
 */
@Controller
@RequestMapping("/service/scheme")
public class SchemeController {
    private final Log logger = LogFactory.getLog(SchemeController.class);

    @Autowired
    private ISchemeService schemeService;

    // 方案List查询（当前账号可以查看到的方案）
    @ResponseBody
    @RequestMapping("/followSchemeListQuery")
    public ResultModel<HashMap<String, Object>> qrySchemeList(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in qrySchemeList----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id

        int page = jsonData.getIntValue("page");
        int rows = jsonData.getIntValue("rows");
        page = page == 0 ? 1 : page;
        rows = rows == 0 ? 20 : rows;

        HashMap<String, Object> servParamMap = new HashMap<String, Object>();
        servParamMap.put("authorName", authorName);
        servParamMap.put("authorId", author);
        servParamMap.put("tenantId", tenantId);
        servParamMap.put("start", (page - 1) * rows);
        servParamMap.put("end", rows);
        servParamMap.put("schemeName", jsonData.get("schemeName"));

        List<Map<String, Object>> list = schemeService.getSchemeList(servParamMap);
        HashMap<String, Object> retMap = new HashMap<String, Object>();
        int total = 0;
        if (null != list) {
            total = list.size();
        }
        retMap.put("rows", list);
        retMap.put("total", total);

        return new ResultModel<HashMap<String, Object>>(retMap);
    }

    // 添加新方案
    @ResponseBody
    @RequestMapping("/followSchemeAdd")
    public ResultModel<HashMap<String, Object>> addFollowScheme(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in addFollowScheme----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        HashMap<String, Object> servParamMap = new HashMap<String, Object>();

        // 入参校验
        String schemeName = jsonData.getString("schemeName");
        if (StringUtils.isBlank(schemeName)) {
            logger.warn("----no schemeName node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案名称为空！"));
        }
        String schemeType = jsonData.getString("schemeType");
        if (StringUtils.isBlank(schemeType)) {
            logger.warn("----no schemeType node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案类型为空！"));
        }
        String opCode = jsonData.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            logger.warn("----no opCode node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonData.getString("opType");
        if (StringUtils.isBlank(opType)) {
            logger.warn("----no opType node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonData.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            logger.warn("----no opNote node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }
        try {
            int schemeCycle = jsonData.getIntValue("schemeCycle");
            if (schemeCycle <= 0) {
                logger.warn("---- schemeCycle <= 0...schemeCycle==" + schemeCycle);
                throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                        "入参校验失败，随访周期为空或值小于0！"));
            }
            int schemeTaskNum = jsonData.getIntValue("schemeTaskNum");
            if (schemeTaskNum <= 0) {
                logger.warn("---- schemeTaskNum <= 0...");
                throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                        "入参校验失败，随访总次数为空或值小于0！"));
            }
            int schemeWarnDay = jsonData.getIntValue("schemeWarnDay");
            if (schemeWarnDay <= 0) {
                logger.warn("---- schemeWarnDay <= 0...");
                throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                        "入参校验失败，随访警戒期为空或值小于0！"));
            }
            int schemeWindowDay = jsonData.getIntValue("schemeWindowDay");
            if (schemeWindowDay <= 0) {
                logger.warn("---- schemeWindowDay <= 0...");
                throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                        "入参校验失败，随访窗口期为空或值小于0！"));
            }
            servParamMap.put("schemeCycle", schemeCycle);
            servParamMap.put("schemeTaskNum", schemeTaskNum);
            servParamMap.put("schemeWarnDay", schemeWarnDay);
            servParamMap.put("schemeWindowDay", schemeWindowDay);
        } catch (AppException ex) {
            throw (ex);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参校验失败，数据格式有误！"));
        }
        servParamMap.put("schemeType", schemeType);
        servParamMap.put("schemeName", schemeName);
        servParamMap.put("schemeIntroduce", jsonData.getString("schemeIntroduce"));

        servParamMap.put("authorName", authorName);
        servParamMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        servParamMap.put("authorId", author);
        servParamMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        servParamMap.put("tenantId", tenantId);
        servParamMap.put("opCode", opCode);
        servParamMap.put("opNote", opNote);
        servParamMap.put("opType", opType);

        String schemeId = schemeService.addSchemeInfo(servParamMap);
        HashMap<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("schemeId", schemeId);

        return new ResultModel<HashMap<String, Object>>(retMap);
    }

    // 删除方案
    @ResponseBody
    @RequestMapping("/followSchemeRemove")
    public ResultModel<Map<String, Object>> delFollowScheme(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in delFollowScheme----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id

        Map<String, Object> servParamMap = new HashMap<String, Object>();

        // 入参校验
        String opCode = jsonData.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            logger.warn("----no opCode node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonData.getString("opType");
        if (StringUtils.isBlank(opType)) {
            logger.warn("----no opType node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonData.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            logger.warn("----no opNote node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }
        String schemeId = jsonData.getString("schemeId");
        if (StringUtils.isBlank(schemeId)) {
            logger.warn("----no schemeId node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案id为空！"));
        }

        servParamMap.put("authorName", authorName);
        servParamMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        servParamMap.put("authorId", author);
        servParamMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        servParamMap.put("tenantId", tenantId);
        servParamMap.put("opCode", opCode);
        servParamMap.put("opNote", opNote);
        servParamMap.put("opType", opType);
        servParamMap.put("schemeId", schemeId);

        schemeService.delSchemeInfo(servParamMap);

        Map<String, Object> retout = new HashMap<String, Object>();
        return new ResultModel<Map<String, Object>>(retout);
    }

    // 更新方案
    @ResponseBody
    @RequestMapping("/followSchemeModify")
    public ResultModel<Map<String, Object>> modFollowSchemeInfo(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in modFollowSchemeInfo----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id

        Map<String, Object> servParamMap = new HashMap<String, Object>();

        // 入参校验
        String opCode = jsonData.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            logger.warn("----no opCode node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonData.getString("opType");
        if (StringUtils.isBlank(opType)) {
            logger.warn("----no opType node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonData.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            logger.warn("----no opNote node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }
        String schemeId = jsonData.getString("schemeId");
        if (StringUtils.isBlank(schemeId)) {
            logger.warn("----no schemeId node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案id为空！"));
        }
        String schemeName = jsonData.getString("schemeName");
        if (StringUtils.isBlank(schemeName)) {
            logger.warn("----no schemeName node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，新方案名称为空！"));
        }

        servParamMap.put("authorName", authorName);
        servParamMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        servParamMap.put("authorId", author);
        servParamMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        servParamMap.put("tenantId", tenantId);
        servParamMap.put("opCode", opCode);
        servParamMap.put("opNote", opNote);
        servParamMap.put("opType", opType);
        servParamMap.put("schemeId", schemeId);
        servParamMap.put("schemeName", schemeName);

        schemeService.modSchemeInfo(servParamMap);

        Map<String, Object> retout = new HashMap<String, Object>();
        return new ResultModel<Map<String, Object>>(retout);
    }

    // 方案下的任务list
    @ResponseBody
    @RequestMapping("/followSchemeTaskListQuery")
    public ResultModel<HashMap<String, Object>> qryFollowSchemeTaskList(@RequestBody String arg) throws Exception {
        logger.warn("in qryFollowSchemeTaskList----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }

        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        String fuSchemeId = jsonData.getString("followSchemeId");
        if (null == fuSchemeId || "".equals(fuSchemeId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入方案id或值为空！"));
        }
        int schemeVersion = jsonData.getIntValue("schemeVersion");
        paramMap.put("fuSchemeId", fuSchemeId);
        paramMap.put("schemeVersion", schemeVersion);

        List<Object> ptList = schemeService.qrySchemeTaskList(paramMap);
        HashMap<String, Object> retMap = new HashMap<String, Object>();
        int total = 0;
        if (null != ptList) {
            total = ptList.size();
        }
        retMap.put("total", total);
        retMap.put("rows", ptList);

        logger.warn("out----retMap==" + retMap.toString());
        return new ResultModel<HashMap<String, Object>>(retMap);
    }

    // 更新方案下某个任务
    @ResponseBody
    @RequestMapping("/followSchemeTaskInfoModify")
    public ResultModel<Map<String, Object>> modFollowSchemeTaskInfo(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in modFollowSchemeTaskInfo----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        JSONObject jsonOprInfo = jsonData.getJSONObject(Constants.INF_INPUTPARAM_OPRINFO_FIELD);
        if (null == jsonOprInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无oprInfo节点！"));
        }
        JSONObject jsonTaskInfo = jsonData.getJSONObject("taskInfo");
        if (null == jsonTaskInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无taskInfo节点！"));
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        Map<String, Object> oprInfoMap = jsonOprInfo;

        // 入参校验
        String opCode = jsonOprInfo.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            logger.warn("----no opCode node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonOprInfo.getString("opType");
        if (StringUtils.isBlank(opType)) {
            logger.warn("----no opType node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonOprInfo.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            logger.warn("----no opNote node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }
        String schemeId = jsonTaskInfo.getString("schemeId");
        if (StringUtils.isBlank(schemeId)) {
            logger.warn("----no schemeId node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案id为空！"));
        }
        String taskId = jsonTaskInfo.getString("taskId");
        if (StringUtils.isBlank(taskId)) {
            logger.warn("----no taskId node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，任务id为空！"));
        }

        oprInfoMap.put("authorName", authorName);
        oprInfoMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        oprInfoMap.put("authorId", author);
        oprInfoMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        oprInfoMap.put("tenantId", tenantId);
        jsonData.put(Constants.INF_INPUTPARAM_OPRINFO_FIELD, oprInfoMap);

        schemeService.modSchemeTaskInfo(jsonData);

        Map<String, Object> retout = new HashMap<String, Object>();
        return new ResultModel<Map<String, Object>>(retout);
    }

    // 删除方案下某个任务
    @ResponseBody
    @RequestMapping("/followSchemeTaskInfoDel")
    public ResultModel<Map<String, Object>> delFollowSchemeTaskInfo(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in delFollowSchemeTaskInfo----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        JSONObject jsonOprInfo = jsonData.getJSONObject(Constants.INF_INPUTPARAM_OPRINFO_FIELD);
        if (null == jsonOprInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无oprInfo节点！"));
        }
        JSONObject jsonTaskInfo = jsonData.getJSONObject("taskInfo");
        if (null == jsonTaskInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无taskInfo节点！"));
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        Map<String, Object> oprInfoMap = jsonOprInfo;

        // 入参校验
        String opCode = jsonOprInfo.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            logger.warn("----no opCode node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonOprInfo.getString("opType");
        if (StringUtils.isBlank(opType)) {
            logger.warn("----no opType node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonOprInfo.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            logger.warn("----no opNote node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }
        String schemeId = jsonTaskInfo.getString("schemeId");
        if (StringUtils.isBlank(schemeId)) {
            logger.warn("----no schemeId node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案id为空！"));
        }
        String taskId = jsonTaskInfo.getString("taskId");
        if (StringUtils.isBlank(taskId)) {
            logger.warn("----no taskId node...");
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，任务id为空！"));
        }

        oprInfoMap.put("authorName", authorName);
        oprInfoMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        oprInfoMap.put("authorId", author);
        oprInfoMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        oprInfoMap.put("tenantId", tenantId);
        jsonData.put(Constants.INF_INPUTPARAM_OPRINFO_FIELD, oprInfoMap);

        schemeService.delSchemeTaskInfo(jsonData);

        Map<String, Object> retout = new HashMap<String, Object>();
        return new ResultModel<Map<String, Object>>(retout);
    }

    // 提交整个方案更改的内容（即将方案更改的内容生效应用）
    @ResponseBody
    @RequestMapping("/followSchemeSubmit")
    public ResultModel<Map<String, Object>> commitFollowScheme(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in commitFollowScheme----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        JSONObject jsonOprInfo = jsonData.getJSONObject(Constants.INF_INPUTPARAM_OPRINFO_FIELD);
        if (null == jsonOprInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无oprInfo节点！"));
        }
        JSONObject schemeInfo = jsonData.getJSONObject("schemeInfo");
        if (null == schemeInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                    "入参节点错误，无schemeInfo节点！"));
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        Map<String, Object> oprInfoMap = jsonOprInfo;

        // 入参校验
        String opCode = jsonOprInfo.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonOprInfo.getString("opType");
        if (StringUtils.isBlank(opType)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonOprInfo.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }
        String schemeId = schemeInfo.getString("schemeId");
        if (StringUtils.isBlank(schemeId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案id为空！"));
        }

        oprInfoMap.put("authorName", authorName);
        oprInfoMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        oprInfoMap.put("authorId", author);
        oprInfoMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        oprInfoMap.put("tenantId", tenantId);
        jsonData.put(Constants.INF_INPUTPARAM_OPRINFO_FIELD, oprInfoMap);

        schemeService.submitSchemeInfo(jsonData);

        Map<String, Object> retout = new HashMap<String, Object>();
        return new ResultModel<Map<String, Object>>(retout);
    }

    // 随访任务内容模板列表查询
    @ResponseBody
    @RequestMapping("/taskContentTemplList")
    public Object listTaskContentTempl(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in listTaskContentTempl----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }

        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }

        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        String flag = jsonData.getString("flag");
        if (!StringUtils.isEmpty(flag)) {
            paramMap.put("classId", flag);
        }

        paramMap.put("authorName", authorName);
        paramMap.put("authorId", author);
        paramMap.put("tenantId", tenantId);

        Map<String, Object> retMap = schemeService.listTaskContentTempl(paramMap);

        return new ResultModel<Map<String, Object>>(retMap);
    }

    // 给当前方案当前任务新增某项任务内容
    @ResponseBody
    @RequestMapping("/schemeTaskContAdd")
    public ResultModel<Map<String, Object>> saveSchemeTaskCont(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in saveSchemeTaskCont----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        JSONObject jsonOprInfo = jsonData.getJSONObject(Constants.INF_INPUTPARAM_OPRINFO_FIELD);
        if (null == jsonOprInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无oprInfo节点！"));
        }
        JSONObject jsonTaskInfo = jsonData.getJSONObject("taskInfo");
        if (null == jsonTaskInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无taskInfo节点！"));
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        Map<String, Object> oprInfoMap = jsonOprInfo;

        // 入参校验
        String opCode = jsonOprInfo.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonOprInfo.getString("opType");
        if (StringUtils.isBlank(opType)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonOprInfo.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }

        // 任务信息
        String schemeId = jsonTaskInfo.getString("schemeId");
        if (StringUtils.isBlank(schemeId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案id为空！"));
        }
        String taskId = jsonTaskInfo.getString("taskId");
        if (StringUtils.isBlank(taskId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，任务id为空！"));
        }
        String templId = jsonTaskInfo.getString("templId");
        if (StringUtils.isBlank(templId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，内容模板id为空！"));
        }
        String templVersion = jsonTaskInfo.getString("templVersion");
        if (StringUtils.isBlank(templVersion)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，内容模板版本为空！"));
        }
        String seqNo = jsonTaskInfo.getString("seqNo");
        if (StringUtils.isBlank(seqNo)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，内容模板优先级序号为空！"));
        }

        oprInfoMap.put("authorName", authorName);
        oprInfoMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        oprInfoMap.put("authorId", author);
        oprInfoMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        oprInfoMap.put("tenantId", tenantId);
        jsonData.put(Constants.INF_INPUTPARAM_OPRINFO_FIELD, oprInfoMap);

        Map<String, Object> retout = schemeService.saveSchemeTaskCont(jsonData);
        return new ResultModel<Map<String, Object>>(retout);
    }

    // 删除当前方案当前任务下某项任务内容
    @ResponseBody
    @RequestMapping("/schemeTaskContDel")
    public ResultModel<Map<String, Object>> removeSchemeTaskCont(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in removeSchemeTaskCont----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        JSONObject jsonOprInfo = jsonData.getJSONObject(Constants.INF_INPUTPARAM_OPRINFO_FIELD);
        if (null == jsonOprInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无oprInfo节点！"));
        }
        JSONObject jsonTaskInfo = jsonData.getJSONObject("taskInfo");
        if (null == jsonTaskInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无taskInfo节点！"));
        }

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String author = (String) sessionMap.get("authorId");
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        Map<String, Object> oprInfoMap = jsonOprInfo;

        // 入参校验
        String opCode = jsonOprInfo.getString("opCode");
        if (StringUtils.isBlank(opCode)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作编码为空！"));
        }
        String opType = jsonOprInfo.getString("opType");
        if (StringUtils.isBlank(opType)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作类型为空！"));
        }
        String opNote = jsonOprInfo.getString("opNote");
        if (StringUtils.isBlank(opNote)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作备注为空！"));
        }

        // 任务信息
        String schemeId = jsonTaskInfo.getString("schemeId");
        if (StringUtils.isBlank(schemeId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，方案id为空！"));
        }
        String taskId = jsonTaskInfo.getString("taskId");
        if (StringUtils.isBlank(taskId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，任务id为空！"));
        }
        String taskContId = jsonTaskInfo.getString("taskContId");
        if (StringUtils.isBlank(taskContId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "任务内容id不能为空！"));
        }

        oprInfoMap.put("authorName", authorName);
        oprInfoMap.put("updateTime", DateUtil.getTimeString(DateUtil.getSysDateA()));// 操作时间
        oprInfoMap.put("authorId", author);
        oprInfoMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水
        oprInfoMap.put("tenantId", tenantId);
        jsonData.put(Constants.INF_INPUTPARAM_OPRINFO_FIELD, oprInfoMap);

        Map<String, Object> retout = schemeService.removeSchemeTaskCont(jsonData);
        return new ResultModel<Map<String, Object>>(retout);
    }

    // 模板问卷内容查询
    @ResponseBody
    @RequestMapping("/queryTemplCont")
    public ResultModel<Object> queryTemplCont(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in queryTemplCont----arg==" + arg);

        // 基础校验
        ToolUtils.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 入参信息校验
        Map<String, Object> chkVar = new HashMap<String, Object>();
        chkVar.put("templId", "标记模板id");
        ToolUtils.checkInputInfo("data", mapIn, chkVar);
        chkVar.clear();
        chkVar.put("templVersion", "模板版本");
        ToolUtils.checkInputIntegerGe("data", mapIn, chkVar);

        // 登录信息
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        mapIn.put("tenantId", sessionMap.get("tenantId"));

        Map<String, Object> retMap = schemeService.listTemplCont(mapIn);

        return new ResultModel<Object>(retMap);
    }

}
